<template>
  <div>
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="用户信息">
          <el-input
              class="w-[280px]"
              v-model="queryParams.keyword"
              placeholder="请输入账号/学号/联系电话"
              clearable
              @keyup.enter="resetPage"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select class="w-[280px]" v-model="queryParams.status" placeholder="请选择状态">
            <el-option label="待审核" value="0" />
            <el-option label="审核通过" value="1" />
            <el-option label="审核拒绝" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <el-table size="large" v-loading="pager.loading" :data="pager.lists">
        <el-table-column label="头像" min-width="100">
          <template #default="{ row }">
            <el-avatar :src="row.avatar" :size="50" />
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="name" min-width="100" />
        <el-table-column label="性别" prop="sex" min-width="60">
          <template #default="{row}">
            <span v-if="row.sex==1">男</span>
            <span v-if="row.sex==0">女</span>
          </template>
        </el-table-column>
        <el-table-column label="年龄" prop="age" min-width="60" />
        <el-table-column label="学号" prop="studentNum" min-width="100" />
        <el-table-column label="学院" prop="college" min-width="100" />
        <el-table-column label="年级" prop="grade" min-width="80" />
        <el-table-column label="专业" prop="major" min-width="120" />

        <el-table-column label="联系电话" prop="contact" min-width="90" />
        <el-table-column label="类型" prop="type" min-width="100">
          <template #default="{ row }">
            <span v-if="row.type ==1 ">社团管理员</span>
            <span v-if="row.type ==2 ">社团管理员</span>
            <span v-if="row.type ==3 ">社团团员</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" min-width="120">
          <template #default="{ row }">
            <el-tag v-if="row.status == 0">待审核</el-tag>
            <el-tag type="success" v-else-if="row.status == 1">通过</el-tag>
            <el-tag type="danger" v-else-if="row.status == 2">拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button
                :disabled="row.status == 1"
                type="primary"
                link
                @click="handleExamine(row)"
            >
              审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
  </div>
</template>
<script lang="ts" setup name="consumerLists">
import { usePaging } from '@/hooks/usePaging'
import {memberApplyLists, memberExamine} from "@/api/member";
import {ElMessageBox} from "element-plus";

const queryParams = reactive({
  keyword: '',
  status: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: memberApplyLists,
  params: queryParams
})
const handleExamine = async (obj: any) => {
    // 显示确认框
  await ElMessageBox.confirm(
      '审核社团社员【'+obj.name+'】队员的报名申请是否通过？',
      '审核提示',
      {
        confirmButtonText: '通过',
        cancelButtonText: '不通过',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          console.log(action)
          if (action === 'confirm') {
            done();
            handleConfirm(obj,1);
          } else if (action === 'cancel') {
            done();
            handleCancel(obj,2);
          } else {
            // 这里 action 是关闭图标触发的，可自定义标识
            done();
          }
        }
      }
  );
}
const handleConfirm = async (obj:any,status : number) => {
  await memberExamine({ id:obj.id,status:status })
  getLists()
};

const handleCancel = async (obj:any,status : number) => {
  await memberExamine({ id:obj.id,status:status })
  getLists()
};
onActivated(() => {
  getLists()
})

getLists()
</script>
